package actions;

//~--- non-JDK imports --------------------------------------------------------

import beans.*;

import client.TopicsPosts;

import dto.*;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

//~--- JDK imports ------------------------------------------------------------

import java.awt.image.BufferStrategy;


import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateTopicAction extends org.apache.struts.action.Action {
    public CreateTopicAction() {}

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        System.out.println("inside add topic action");
        Topics   topic   = new Topics();
        Posts    post    = new Posts();
        String   name    = ((CreateTopicActionForm) form).getTopicname();
        System.out.println("name of topic="+name);
        String   content = ((CreateTopicActionForm) form).getContent();
        System.out.println("content of topic="+content);
        System.out.println(" using request sec id is  "+request.getParameter("secid"));
        topic.setTopicname(name);
        String secID =  request.getParameter("secid");
        System.out.println("the id from url in action  :"+secID);
        Sections section = new Sections();
        //Integer   sectionid = ((CreateTopicActionForm) form).getSectionid();
        section.setSectionid(Integer.parseInt(secID));    // this must get section id from request
        System.out.println("iddddddddddddd od ssssssssseccccction="+section.getSectionid());
        topic.setSections(section);

        JfMembers memb = (JfMembers) request.getSession().getAttribute("userBean");
        System.out.println("user in create topicn action:"+memb.getMemberId());
        topic.setJfMembers(memb);

        TopicsPosts model = new TopicsPosts();

        post.setJfMembers(memb);
        post.setContent(content);
        System.out.println("name id in action="+memb.getMemberId());
        // model.addTopic(topic, post);
        Integer topicId = model.addTopic(topic, post);
        ActionForward forward = new ActionForward();
        if(topicId !=null)
        {
            System.out.println(".... going to goodtopic");
           //request.setAttribute("section", secID);
           response.sendRedirect("DisplayTopicAction.do?section="+secID);
           //forward = mapping.findForward("goodtopic");
           return null;
        }
        else
        {
            System.out.println(".... going to baaaaaaaaaaadtopic");
            forward = mapping.findForward("badtopic");
           return forward;
        }
        //return null;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
